const app = getApp();

Page({
  data: {
    loading: true,
    appConfig:[],
    keyword: "",
    stores: [],
    shop: [],
    page:0
  },
  //监听页面加载
  onLoad: function (event) {
    this.setData({
      keyword: event.keyword
    })
  },
  //加载完成显示
  onShow: function () {
    this.getStore();
    this.getItem();
  },
  //加载完成
  onReady: function () {
    this.setData({
      appConfig: app.globalData.appConfig
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
    this.setData({
      page: 0,
      loading: true,
      stores: [],
      shop: []
    });
    this.getStore();
    this.getItem();
  },
  //底部加载更多
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    this.getItem();
  },
  //获取商品列表
  getStore: function () {
    if(!app.util().isNull(this.data.keyword)){
      var parms = {
        page:1,
        cate_id: 0,
        cate_sid: 0,
        keyword: this.data.keyword,
      }
      app.api().Get('api/v4/allwin/store/lists',parms,(result) =>{
        this.setData({
          stores: result.data,
        });
      })
    }
  },
  //获取商品列表
  getItem: function () {
    if (this.data.loading && !app.util().isNull(this.data.keyword)) {
      var param = {
        signkey: app.util().getRandom(12),
        keyword: this.data.keyword,
        store_id:0,
        types:0,
        page: this.data.page + 1,
      }
      app.api().Get('api/v4/allwin/shop/goods',param,(result) =>{
        var shop = this.data.shop;
        for (let i in result.data) {
          shop.push(result.data[i]);
        }
        this.setData({
          shop: shop,
          page: param.page,
          loading: false
        });
      })
    }else{
      this.setData({
        loading: false
      });
    }
  },
  //搜索
  onSearch: function (event) {
    if (app.util().isNull(event.detail)) {
      app.wxAlert('请输入搜索关键词')
      this.setData({
        loading: false,
      })
    } else {
      this.setData({
        loading: true,
        page: 0,
        keyword:event.detail,
        stores: [],
        shop: [],
      })
      this.getStore();
      this.getItem();
    }
  },
})